var log = require("../../utils/log.js");
var Cmd = require("../Cmd.js");
var Respones = require("../Respones.js");
var Stype = require("../Stype.js");
var Cmd = require("../Cmd.js");
var utils = require("../../utils/utils.js");

require("./game_system_proto.js");
var system_model = require("./game_system_model.js");

function get_game_info(session, uid, proto_type, body){
	system_model.get_game_info(uid, function(body){
		session.send_cmd(Stype.GameSystem, Cmd.GameSystem.GET_GAME_INFO, body, uid, proto_type);
	});
}

function get_login_bonues_info(session, uid, proto_type, body){
	system_model.get_login_bonues_info(uid, function(body){
		session.send_cmd(Stype.GameSystem, Cmd.GameSystem.LOGIN_BONUES_INFO, body, uid, proto_type);
	});
}

function recv_login_bonues(session, uid, proto_type, body){
	//验证数据合法性
	if(!body){
		session.send_cmd(Stype.GameSystem, Cmd.GameSystem.RECV_LOGIN_BUNUES, Respones.INVALID_PARAMS, uid, proto_type);
		return;
	}

	var bonuesid = body;
	system_model.recv_login_bonues(uid, bonuesid, function(body){
		session.send_cmd(Stype.GameSystem, Cmd.GameSystem.RECV_LOGIN_BUNUES, body, uid, proto_type);
	});
}

function get_world_rank_info(session, uid, proto_type, body){
	system_model.get_world_rank_info(uid, function(body){
		session.send_cmd(Stype.GameSystem, Cmd.GameSystem.GET_WORLD_RANK_INFO, body, uid, proto_type);
	});
}


var service = {
	name: "game_system_service", // 服务名称
	is_transfer: false,		//是否为转发模块

	// 收到连接的客户端发给我们的数据
	on_recv_player_cmd: function(session, stype, ctype, body, utag, proto_type, raw_cmd) {
		log.info("server_session: " + session.uid + " stype: " + stype + " ctype: " + ctype + " utag: " + utag + " proto_type: " + proto_type + "body: " + body);
		switch(ctype){
			case Cmd.GameSystem.GET_GAME_INFO:
				get_game_info(session, utag, proto_type, body);
			break;
			case Cmd.GameSystem.LOGIN_BONUES_INFO:
				get_login_bonues_info(session, utag, proto_type, body);
			break;
			case Cmd.GameSystem.RECV_LOGIN_BUNUES:
				recv_login_bonues(session, utag, proto_type, body);
			break;
			case Cmd.GameSystem.GET_WORLD_RANK_INFO:
				get_world_rank_info(session, utag, proto_type, body);
			break;
		}
		
	},

	// 收到连接的服务器发给我们的数据
	on_recv_server_return: function(session, stype, ctype, body, utag, proto_type, raw_cmd) {
	},

	// 每个服务连接丢失后调用,被动丢失连接
	on_player_disconnect: function(stype, session) {
	},
};

module.exports = service;